import {searchPage,addEntity,searchOrder,searchOrderDetail,searchOne,updateEntity,deleteApplyByApplyIdList} from '../../../api/b2cReturns'

let complaint = {
    data() {
        return {
            //日期组件
            createTimeOptions:'',
            approvalTimeOptions:'',
            //table的list
            tableData: [],
            total:0,
            searchParams:{
                currentPage :1,
                pageSize:5
            },
            approvalStatusOptions:[
                {
                    value:null,
                    label:"全部"
                },
                {
                    value: 0,
                    label: '未审批'
                }, {
                    value: 1,
                    label: '已通过'
                }, {
                    value:2,
                    label: '未通过'
                }
            ],
            goodStatusOptions:[
                {
                    value: 0,
                    label: '退货'
                }, {
                    value: 1,
                    label: '同机型换货'
                }, {
                    value:2,
                    label: '不同机型换货'
                }
            ],
            dialogVisible:false,
            formData:{},
            editFormVisible:false,
            editApplyForm:{
                applyId:'',
                sellCode:'',
                complaintId:'',
                producer:'admin',
                producDept:'售后部门',
                remarks:'',
                status:0,
                overhaulId:'',
                overhaulName:'',
                overhaulTime:'',
                overhaulPhone:'',
                approver:'',
                approvalTime:'',
                approvalIdea:'',
                approvalStatus:0,
                createTime:null,
                entryId:'',
                complaintName:'',
                complaintArray:'',
                complaintRemark:'',
                oneIdea:'',
                oneTime:'',
                twoIdea:'',
                twoTime:'',
                list:[]
            },
            orderData:{
                orderCode:'',
                orderType:'',
                orderAction:'',
                businessType:'',
                orderState:'',
                orderCreateTime:'',
                consigneeName:'',
                memberAccount:'',
                phoneNumber:'',
                cellphoneNumber:'' ,
                postalCode:'',
                deliveryAddress:'',
                deliveryArea:'',
                deliveryWay:'' ,
                deliveryCost:'',
                payWay:'',
                selfPickupTime:'',
                email:'',
                payer:'',
                remark:'',
                sellOrderGoodsList:[],
            },
            editForm:true,
            title:'',
            chooseGoodVisible:false,
            orderGoodDate:[],
            chooseGoodDate:[],
            chooseOrderVisible:false,
            chooseOrderDate:[],
            orderFindDate:[],
            orderTotal:0,
            searchOrderParams:{
                currentPage :1,
                pageSize:5
            },
            deleteIds:[]
        };
    },
    methods:{
        //添加申诉记录单
        addVisible(){
            this.editFormVisible = true;
            this.editForm = false;
            this.title="添加申诉记录单";
            this.findOrder();
        },
        async findOrder(){
            let response =  await searchOrder(this.searchOrderParams);
            this.orderTotal= response.total;
            this.orderFindDate = response.records;
        },
        currentOrderPageChange(currentPage){
            //console.log(currentPage);
            this.searchOrderParams.currentPage = currentPage;
            this.findOrder();
        },
         chooseOrder(){
            this.chooseOrderVisible = true;
        },
        addOrder(order){
            this.orderDetail(order.sellCode);
            this.editApplyForm.sellCode = order.sellCode;
            this.chooseOrderVisible = false;
            this.editApplyForm.list = [];
        },
        async orderDetail(sellCode){
            let response =  await searchOrderDetail(sellCode);
            this.orderData = JSON.parse(JSON.stringify(response))
        },
        async chooseGood(){
            this.chooseGoodVisible = true;
            let good= this;
            await this.orderData.sellOrderGoodsList.forEach(function (value, index, array) {
                let num ={key:'',
                    label:''};
                num.key = value.connectCode;
                num.label = value.goodName;
                good.orderGoodDate.push(num);
            });
            //console.count(good.orderGoodDate);
            if(this.editApplyForm.list !== null){
                this.editApplyForm.list.forEach(function (value) {
                    let num = good.orderGoodDate;
                    good.chooseGoodDate.push(value.connectCode);
                    for(let i = 0;i<num.length;i++){
                        if(value.connectCode === num[i]){
                            num.delete(i);
                        }
                    }
                })
            }
        },
        resetChooseGood(){
            this.chooseGoodVisible = false;
            this.orderGoodDate=[];
            this.chooseGoodDate=[];
        },
        addChooseGood(){
            let good= this;
            this.editApplyForm.list = [];
            this.orderData.sellOrderGoodsList.forEach(function (value, index, array) {
                let num = good.chooseGoodDate;
                for(let i = 0;i<num.length;i++){
                    if(value.connectCode === num[i]){
                        good.editApplyForm.list.push(value);
                    }
                }
            });
            this.resetChooseGood();
        },
        async addApply(){
            let response =  await addEntity(this.editApplyForm);
            this.resetFields();
        },
        //修改
        async updateVisible(apply){
            let response =await searchOne(apply.applyId);
            this.editFormVisible = true;
            this.editForm = false;
            this.title="修改申诉记录单";
            this.findOrder();
            this.editApplyForm=JSON.parse(JSON.stringify(response));
            if(response.sellCode!==null){
                this.orderDetail(response.sellCode);
            }
        },
        async updateApply(){
            this.editApplyForm.approver='';
            this.editApplyForm.approvalTime='';
            this.editApplyForm.approvalIdea='';
            this.editApplyForm.approvalStatus=0;
            let response =  await updateEntity(this.editApplyForm);
            this.resetFields();
        },
        //查看
        async applyDetail(apply){
            let response =await searchOne(apply.applyId);
            this.editFormVisible = true;
            this.title="查看申诉记录单";
            this.editApplyForm=JSON.parse(JSON.stringify(response));
            if(response.sellCode!==null){
                this.orderDetail(response.sellCode);
            }
        },
        //审核跟踪
        async approvalDetail(apply){
            this.editFormVisible = true;
            this.title="审核跟踪";
            let response =await searchOne(apply.applyId);
            this.editApplyForm=JSON.parse(JSON.stringify(response));
            if(response.sellCode!==null){
                this.orderDetail(response.sellCode);
            }
        },
        //复选框的change事件，删除和修改拿到id
        checkBoxSelectionChange(selection){
            this.deleteIds = [];
            let ids=this;
            selection.forEach(function (value, index) {
                ids.deleteIds.push(value.applyId);
            });
            console.log(this.deleteIds);
        },
        async deleteByIds(){
            await deleteApplyByApplyIdList(this.deleteIds);
            this.findPage();
        },
        currentPageChange(currentPage){
            this.searchParams.currentPage = currentPage;
            //console.count(this.searchParams.currentPage);
            this.findPage();
        },
        formatDate(row, column){
            let date = row[column.property];
            if(date === null){
                return date;
            }
            let one = date.indexOf('-');
            let year = date.slice(0,one);
            let month = date.slice(one+1,one+3);
            let day = date.slice(one+4,one+6);
            return year+'年'+month+'月'+day+'日';

        },
       async findPage(){
           let response =  await searchPage(this.searchParams);
               this.total= response.total;
               this.tableData = response.data;
        },
        chooseCreateTime(){
            this.searchParams.startTime=this.createTimeOptions[0];
            this.searchParams.endTime=this.createTimeOptions[1];
        },
        chooseApprovalTime(){
            this.searchParams.approvalStartTime=this.approvalTimeOptions[0];
            this.searchParams.approvalEndTime=this.approvalTimeOptions[1];
        },
        resetForm(){
            this.searchParams={  currentPage :1, pageSize:5};
            this.createTimeOptions ='';
            this.approvalTimeOptions ='';
        },
        resetFields(){
            this.editFormVisible = false;
            this.editForm = true;
            this.orderData={};
            this.editApplyForm={
                applyId:'',
                    orderId:'',
                    complaintId:'',
                    producer:'admin',
                    producDept:'售后部门',
                    remarks:'',
                    status:0,
                    overhaulId:'',
                    overhaulName:'',
                    overhaulTime:'',
                    overhaulPhone:'',
                    approver:'',
                    approvalTime:'',
                    approvalIdea:'',
                    approvalStatus:0,
                    createTime:'',
                    entryId:'',
                    complaintName:'',
                    complaintArray:'',
                    complaintRemark:'',
                    oneIdea:'',
                    oneTime:'',
                    twoIdea:'',
                    twoTime:'',
                    list:[]
            };
            this.findPage();
        }
    },
    created(){
        this.findPage();
    }
};

export default complaint;